var app = angular.module('myApp', ['ui.router','ui.bootstrap', 'ngAnimate']);
app.run(function ($rootScope, $state, $stateParams) {
    $rootScope.state = $state;
    $rootScope.stateParams = $stateParams;
    // 参数的类
    $rootScope.param = {
        fieldName: "",
        value: ""
    };
    // 排序的类
    $rootScope.sorts = {
        field: "",
        direction: ""
    };
    $rootScope.id = "";
    $rootScope.password = "";
    $rootScope.redirectToErrorPage = function () {
        $state.transitionTo("500");
    }

});
// 页面间传值
app.factory('Data', function () {
    var savedData = {}

    function set(data) {
        savedData = data;
    }

    function get() {
        return savedData;
    }

    return {
        set: set,
        get: get
    }
});
app.factory('HttpInterceptor', function ($q, $injector) {
    return {
        request: function (config) {
            return config;
        },
        requestError: function (err) {
            return $q.reject(err);
        },
        response: function (res) {
            return res;
        },
        responseError: function (err) {
            var stateService = $injector.get('$state');
            if (-1 === err.status) {
                // 远程服务器无响应
            } else if (500 === err.status) {
                stateService.go('500');
            } else if (404 === err.status) {
                stateService.go('404');
            }
            return $q.reject(err);
        }
    };
});
// 路由转换
app.config(function ($stateProvider, $urlRouterProvider, $locationProvider,
                     $httpProvider) {
    $httpProvider.interceptors.push('HttpInterceptor');
    $urlRouterProvider.otherwise('/login');
    $stateProvider.state('index', {
        url: '/index',
        views: {
            '': {
                templateUrl: 'partials/Commodity.html',
                controller: ClassListCtrl
            }
        }
    }).state('class', {
        url: '/class',
        views: {
            '': {
                templateUrl: 'partials/class.html',
                controller: StudentCtrl
            }

        }
    }).state('feedback', {
        url: '/feedback',
        views: {
            '': {
                templateUrl: 'partials/feedback.html',
                controller: ClassListCtrl
            }
        }
    }).state('studentAnalysis', {
        url: '/studentAnalysis',
        views: {
            '': {
                templateUrl: 'partials/studentAnalysis.html',
                controller:ClassListCtrl
            }
        }
    }).state('prepareLesson', {
        url: '/prepareLesson',
        views: {
            '': {
                templateUrl: 'partials/prepareLesson.html',
            }
        }
    }).state('homework', {
        url: '/homework',
        views: {
            '': {
                templateUrl: 'partials/homework.html',
                controller: ClassListCtrl
            }
        }
    }).state('feedbackson', {
        url: '/feedbackson',
        views: {
            '': {
                templateUrl: 'partials/feedbackson.html',
                controller: ClassListCtrl
            }
        }
    }).state('addHomework', {
        url: '/addHomework',
        views: {
            '': {
                templateUrl: 'partials/addHomework.html',
                controller: AddHomeworkCtrl
            }
        }
    }).state('afterclassHomework', {
        url: '/afterclassHomework',
        views: {
            '': {
                templateUrl: 'partials/afterclassHomework.html',
                controller: ClassListCtrl
            }
        }
    }).state('addClass', {
        url: '/addClass',
        views: {
            '': {
                templateUrl: 'partials/addClass.html',
                controller: ClassListCtrl
            }
        }
    }).state('addStudent', {
        url: '/addStudent',
        views: {
            '': {
                templateUrl: 'partials/addStudent.html',
                controller: StudentCtrl
            }
        }
    }).state('login', {
        url: '/login',
        views: {
            '': {
                templateUrl: 'partials/login.html',
                controller: LoginCtrl
            }
        }
    }).state('register', {
        url: '/register',
        views: {
            '': {
                templateUrl: 'partials/register.html',
                controller: RegisterCtrl
            }
        }
    }).state('Commodity', {
        url: '/Commodity',
        views: {
            '': {
                templateUrl: 'partials/Commodity.html',
                controller: CommodityCtrl
            }
        }
    }).state('selectedCommodityDetail',{
        url:'/selectedCommodityDetail',
        views:{
            '':{
                templateUrl:'partials/selectedCommodityDetail.html',
                controller:SelectedCommodityDetailCtrl
            }
        }
    }).state('detailClass',{
        url:'/detailClass',
        views:{
            '':{
                templateUrl:'partials/detailClass.html',
                controller:ClassListCtrl
            }
        }
    }).state('studentAnalysisson',{
        url:'/studentAnalysisson',
        views:{
            '':{
                templateUrl:'partials/studentAnalysisson.html',
                controller:StudentCtrl
            }
        }
    })


});
app.filter('Enabled', function () {
    return function (enabled) {
        if (enabled) {
            return '可用';
        } else {
            return '禁用';
        }
    };
});
